<?php
// +----------------------------------------------------------------------+
// | Author:  Evgeny Leontev <eleontev@gmail.com>                         |
// | Copyright (c) 2005 Evgeny Leontev                                    |
// +----------------------------------------------------------------------+
// | This source file is free software; you can redistribute it and/or    |
// | modify it under the terms of the GNU Lesser General Public           |
// | License as published by the Free Software Foundation; either         |
// | version 2.1 of the License, or (at your option) any later version.   |
// |                                                                      |
// | This source file is distributed in the hope that it will be useful,  |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU    |
// | Lesser General Public License for more details.                      |
// +----------------------------------------------------------------------+



/**
 * AppObj is used for all object classes (User, Item, etc.), 
 *
 * @since 21/03/2003
 * @author Evgeny Leontiev <eleontev@gmail.com>
 * @access public
 */

 
class AppObj extends BaseObj
{
	
	var $properties = array();
	var $js;                     // jscript to validate form
	var $errors = false;
	var $hidden = array('id');
	var $manager;

	
	//function AppObj() {
	//	$this->setDefault();
	//}
	
	
	function setError($msg_key) {
		$this->errors[]['msg'] = $msg_key;
	}
	
	
	function setHtmlValues($key) {
		$this->html_values[$key] = &$this->properties[$key];
	}
	
	
	function setDefault($property, $val) {
		//if(isset($this->properties[$property]) && empty($this->properties[$property])) {
			$this->set($property, $val);
		//}
	}
	
	
	function setManager(&$manager) {
		return $manager;
	}
	
	// reassign this func to make some formatting for certain obj
	function _callBack($property, $val) {
		return $val;
	}
	
	
	function set($key_or_arr, $value = false, $strip_vars = 'none') {
		
		if(is_array($key_or_arr)) {
			foreach($key_or_arr as $k => $v) {
				if(!array_key_exists ($k, $this->properties)) { continue; }
				$this->properties[$k] = $this->_callBack($k, $v);
			}
		} else {
			if($value === false) {
				trigger_error(__class__.'::set() - Can\'t assign value for var - <b>'.$value.'</b>. Missing argument 2;');
			} else { 
				$this->properties[$key_or_arr] = $this->_callBack($key_or_arr, $value);
			}
		}
	}
	
	
	// this could be for unset some properties
	// to generate right sql
	function unsetProperties($val) {
		$val = (is_array($val)) ? $val : array($val);
		foreach($val as $v) {
			unset($this->properties[$v]);
		}
	}
	
	
	function get($property = false) {
		return ($property) ? $this->properties[$property] : $this->properties;
	}
}



/*
/*
	function __set($nm, $val) { //print "Setting [$nm] to $val\n<br>"; 
		$this->propeties[$nm] = $this->_callBack($nm, $val);
	}
	
	
	function __get($nm) { //print "Getting [$nm]\n<br>"; 
		if (isset($this->propeties[$nm])) {
			$t = $this->propeties[$nm]; 
		}
		
		if($t === false) {
			return $t; 
		} else {
			echo "Undefined property <b>$nm</b>"; 
		}
	} 
	
	
	function __call($method, $args){
		
		//echo "<pre>"; print_r(get_class_methods($this->manager)); echo "</pre>";
		//echo "<pre>"; print_r(get_object_vars($this->manager)); echo "</pre>";
		
		
		$pr = substr($method, 0, 3); // 'set' or 'get'
		//$property = str_replace($pr, '', $method);
		//preg_match_all("#[A-Z]{1,}[a-z]+#", $property, $match);
		//$property = strtolower(implode('_', $match[0]));
		
		$pattern = "/(set|get)(\w+)([A-Z]{1,}[a-z]+)/";
		$replacement =  "\$2_\$3"; 
		$property = strtolower(preg_replace($pattern, $replacement, $method)); 
		
		$args[0] = $this->_callBack($property, $args[0]);
		
		if($pr == 'set') {
			$this->propeties[$property] = $args[0];
		} elseif ($pr == 'get') {
			return $this->propeties[$property];
		}
	}



	// $param true, false (for server check) or real values
	function stripVars($server_check = false) {
		 
		if(!$this->strip_vars) { return; }
		
		$_ar = array_merge($this->skip_keys, array_keys($this->html_values));
		
		RequestDataUtil::stripVars($this->properties, $_ar, $server_check, $this->wordrap_num);
		RequestDataUtil::stripVarsHtml($this->html_values, array(), $server_check);
	}
*/
	
?>